from typing import List

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from wsc_core import flag
from wsc_core.models import ClassUserBind, QuestionAnswer

app = FastAPI()


class PredictedGrades(BaseModel):
    student_id: str
    grade: int


def predict_student_grade(student_id):
    used_time = 0
    correct_count = 0
    question_answers = QuestionAnswer.select().where(QuestionAnswer.student_id == student_id)
    for question_answer in question_answers:
        used_time += question_answer.used_time
        if question_answer.is_correct:
            correct_count += 1
    # training.model
    return PredictedGrades(
        student_id=student_id,
        grade=0,
    )


@app.get('/api/v1/grade/predict_class_students_grades/{class_uuid}', response_model=List[PredictedGrades])
def predict_class_students_grades(class_uuid):
    ClassUserBinds = ClassUserBind.select().where(ClassUserBind.class_uuid == class_uuid)
    return [predict_student_grade(classUserBind.student_id) for classUserBind in ClassUserBinds]


uvicorn.run(app, host=flag.args.http_address, port=flag.args.http_port)
